package com.tang.rabbitmq.listener;/*
 *   @author Dram
 *   @create 2021-04-16 13:58
 */

import com.rabbitmq.client.Channel;
import com.tang.rabbitmq.pojo.User;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

// 2个消费者
@Component
public class WorkReceiveListener {
    @RabbitListener(queues = "queue_work")
    public void receiveMessage(String msg, Channel channel, Message message) {
        // 只包含发送的消息
        try {
            //处理业务逻辑
            String correlationId = message.getMessageProperties().getCorrelationId();
            System.out.println("1接收到消息：" + msg);
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // channel 通道信息
        // message 附加的参数信息
    }

    @RabbitListener(queues = "queue_work")
    public void receiveMessage2(Object obj, Channel channel, Message message) {
        // 包含所有的信息
        System.out.println("2接收到消息：" + obj);
    }

    @RabbitListener(queues = "queue_fanout1")
    public void receiveMsg1(String msg) {
        System.out.println("队列1接收到消息：" + msg);
    }

    @RabbitListener(queues = "queue_fanout2")
    public void receiveMsg2(String msg) {
        System.out.println("队列2接收到消息：" + msg);
    }


    @RabbitListener(queues = "queue_topic1")
    public void receiveMsg3(String msg, Channel channel, Message message) {
        System.out.println("消费者1接收到：" + msg);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RabbitListener(queues = "queue_topic2")
    public void receiveMsg4(String msg, Channel channel, Message message) {
        System.out.println("消费者2接收到：" + msg);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println(deliveryTag);
        if (deliveryTag<=2){
            try {
                channel.basicAck(deliveryTag,false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            try {
                //第三个参数是否重新入队列  true 重新入队  false 丢弃
                channel.basicNack(deliveryTag,false,false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    @RabbitListener(queues = "queue_confirm")
    public void receiveMsg(User user) {
        System.out.println("接收到的消息为：" + user);
    }

    @RabbitListener(queues = "queue.demo.immediate")
    public void receiveDelayMsg(String msg, Channel channel, Message message) {
        System.out.println(msg);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            channel.basicAck(deliveryTag,false);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
